package com.lin.entity;

import com.lin.utils.CodeEnum;

public class R<T> {
    private Integer code;
    private String msg;
    private T data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public R() {
    }

    private R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    @Override
    public String toString() {
        return "R{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    public static <T> RBuilder<T> builder() {
        return new RBuilder<>();
    }
    public static class RBuilder<T> {
        private Integer code;
        private String msg;
        private T data;
        public RBuilder<T> code(Integer code) {
            this.code = code;
            return this;
        }
        public RBuilder<T> msg(String msg) {
            this.msg = msg;
            return this;
        }
        public RBuilder<T> data(T data) {
            this.data = data;
            return this;
        }

        public R<T> build() {
            return new R<T>(code, msg, data);
        }
    }

    public static <T> R<T> success(T data) {
        return R.<T>builder()
                .code(CodeEnum.SUCCESS.getCode())
                .msg(CodeEnum.SUCCESS.getMsg())
                .data(data)
                .build();
    }
    // 响应成功, 不携带 data 数据
    public static <T> R<T> success() {
        return R.<T>builder()
                .code(CodeEnum.SUCCESS.getCode())
                .msg(CodeEnum.SUCCESS.getMsg())
                .build();
    }
    // 返回状态 失败, 默认返回  code 为400, 默认返回 msg 为失败
    public static <T> R<T> fail() {
        return R.<T>builder()
                .code(CodeEnum.FAIL.getCode())
                .msg(CodeEnum.FAIL.getMsg())
                .build();
    }
    // 返回状态 失败, 可以在 controller 层指定 code 和 msg
    public static <T> R<T> fail(CodeEnum codeEnum) {
        return R.<T>builder()
                .code(codeEnum.getCode())
                .msg(codeEnum.getMsg())
                .build();
    }
}
